package com.toocms.jinjuhang.ui.mine.order

import android.graphics.Typeface
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.StyleSpan
import com.google.android.material.tabs.TabLayout
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.databinding.FgtMyOrderBinding
import com.toocms.jinjuhang.ui.base.BaseFgt

class MyOrderFgt : BaseFgt<FgtMyOrderBinding, MyOrderModel>() {
    override fun onFragmentCreated() {
        topBar.setTitle(R.string.str_my_order)
        binding.filterTl.apply {
            addTab(
                newTab().setText(R.string.str_all).setTag(OrderStatus.ALL)
            )
            addTab(
                newTab().setText(R.string.str_await_payment).setTag(OrderStatus.AWAIT_PAYMENT)
            )
            addTab(
                newTab().setText(R.string.str_await_shipments).setTag(OrderStatus.AWAIT_SHIPMENTS)
            )
            addTab(
                newTab().setText(R.string.str_await_receiving).setTag(OrderStatus.AWAIT_RECEIVING)
            )
            addTab(
                newTab().setText(R.string.str_await_evaluate).setTag(OrderStatus.AWAIT_EVALUATE)
            )
            addTab(
                newTab().setText(R.string.str_after_sale).setTag(OrderStatus.AFTER_SALE)
            )
            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabSelected(tab: TabLayout.Tab?) {
                    tab?.apply {
                        val titleSpan = SpannableStringBuilder(text.toString())
                        titleSpan.setSpan(
                            StyleSpan(Typeface.BOLD),
                            0,
                            titleSpan.length,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                        )
                        text = titleSpan
                        if (tag is OrderStatus) {
                            viewModel.currentOrderStatus = tag as OrderStatus
                        }
                    }

                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.apply {
                        text = text.toString()
                    }
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })
            val orderStatus = OrderStatus.code2Status(
                arguments?.getInt(Constants.BundleKey.KEY_ORDER_STATUS) ?: OrderStatus.ALL.code
            )
            for (i in 0 until tabCount) {
                val tab = getTabAt(i)
                if (tab?.tag == orderStatus) selectTab(tab, true)
            }

        }
    }

    override fun getLayoutResId() = R.layout.fgt_my_order

    override fun getVariableId() = BR.myOrderModel

    override fun viewObserver() {
        viewModel.stopRefreshOrLoadSingleLiveEvent.observe(this) {
            binding.refreshSrl.finishRefresh()
            binding.refreshSrl.finishLoadMore()
        }
    }
}